using System;
using System.Timers;
using System.Xml;

using umlungu.engineX;
using umlungu.engineX.winserviceX;

/*	-----------------------------------------------------------------------	
	Copyright:	umlungu consulting (pty) ltd
	Author:		Alan Benington
	Started:	2007-10-27
	Status:		beta
	Version:	2.0.0
	Build:		20071027
	License:	GNU General Public License
	-----------------------------------------------------------------------	*/

/*	-----------------------------------------------------------------------	
	Development Notes:
	==================
	20071027:	Start from NMScheduler
	20071101:	Moved this from project into engine.
	-----------------------------------------------------------------------	*/

namespace umlungu.engineX.winserviceX {
	/// <summary>
	/// A Windows Service that is a web service poller.
	/// <para>This service polls a web service to do jobs at intervals:</para>
	/// <para>- This has been separated from the general x_winservice class.</para>
	///	<para>- This is a base class to be extended by an actual implementation of the web service polling.</para>
	/// </summary>
	public class x_poller : x_winservice {
		#region Invisible properties
		private const string CONFIG_ROOT = "pollerX";
		private const string CONFIG_INTERVAL = "@IntervalMinutes";
		private const string DEFAULT_INTERVAL_MINUTES = "5";
		#endregion

		#region Visible properties
		private string webServiceUrl;
		/// <summary>The url of the web service in config</summary>
		/// <value>A url string</value>
		public string WebServiceUrl {
			set { webServiceUrl = value; }
			get { return webServiceUrl; }
		}
		/// <summary>The service configuration element</summary>
		/// <value>an XmlElment of the config object</value>
		public XmlElement ServiceConfig {
			get { return Config.Element(String.Concat(CONFIG_ROOT, "/", ServiceName)); }
		}
		#endregion

		#region Constructors/Destructors
		/// <overloads>Constructor</overloads>
		/// <summary>Default constructor</summary>
		public x_poller() : base("x_poller") {
		}
		/// <summary>Default constructor</summary>
		public x_poller(string thisname) : base(thisname) {
		}
		#endregion

		#region Public methods
		#endregion
		
		#region Override methods
		/// <summary>
		/// Overrides geting the timer interval
		/// </summary>
		protected override double GetTimerInterval() {
			string configInterval = Config.Value(String.Concat(CONFIG_ROOT, "/", ServiceName, "/", CONFIG_INTERVAL));
			double interval = Convert.ToDouble((configInterval != null) ? configInterval : DEFAULT_INTERVAL_MINUTES) * 60000;
			return interval;
		}
		/// <summary>
		/// Does the work of the service. The timer in the base service invokes this method
		/// </summary>
		protected override void RunCommands() {
			try {
				base.RunCommands();	// logs the event
			} catch(Exception e) {
				WriteLog(String.Concat("General error: ", e.Message));
			}
		}
		#endregion

		#region Private methods
		#endregion
	}
}